import pandas as pd
import requests

# 读取Excel文件
file_path = 'text1.xlsx'  # 替换为你的Excel文件路径
df = pd.read_excel(file_path)

# 定义一个函数来获取音标
def get_phonetic_transcription(word):
    try:
        response = requests.get(f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}')
        response.raise_for_status()  # 如果请求失败，抛出HTTPError异常
        data = response.json()
        if data and data[0] and 'phonetic' in data[0][0]:
            return data[0][0]['phonetic']
        else:
            return None
    except Exception as e:
        print(f"Error fetching phonetic transcription for '{word}': {e}")
        return None

# 应用函数到A列，并将结果存储到B列
df['B列'] = df['A列'].apply(get_phonetic_transcription)

# 将结果写回到Excel文件
df.to_excel(file_path, index=False)